﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CoreData;
using System.Reflection;
using SchoolManagementSystem.Controls;

namespace SchoolManagementSystem.Forms.BusinessForm
{
    public partial class frmCashBookFirstPeriod : DevExpress.XtraEditors.XtraForm
    {
        public frmCashBookFirstPeriod()
        {
            InitializeComponent();
            commandBarUC1.cmdnew.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(cmdnew_ItemClick);
            commandBarUC1.cmdsave.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(cmdsave_ItemClick);
            commandBarUC1.cmdrefresh.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(cmdrefresh_ItemClick);
            commandBarUC1.cmdcopy.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
            commandBarUC1.cmdsave.Enabled = false;
            spinEdit_VND.Properties.DisplayFormat.FormatString = "{0:0,0.###}";
            spinEdit_VND.Properties.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Custom;
            spinEdit_USD.Properties.DisplayFormat.FormatString = "{0:0,0.###}";
            spinEdit_USD.Properties.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Custom;

            spinEdit_VND.Properties.Mask.EditMask = "[0-9]+";
            spinEdit_VND.Properties.Mask.MaskType = DevExpress.XtraEditors.Mask.MaskType.RegEx;
            spinEdit_USD.Properties.Mask.EditMask = "[0-9]+";
            spinEdit_USD.Properties.Mask.MaskType = DevExpress.XtraEditors.Mask.MaskType.RegEx;
            LoadData();
        }

        CashBookFirstPeriod c;
        private void cmdnew_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            ID = 0;      
           
            DisPlay();
       
        }
        private void cmdsave_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            c.Year = int.Parse(comboBoxEdit_Year.EditValue.ToString());
            c.Month = int.Parse(comboBoxEdit_Month.EditValue.ToString());
            c.MoneyUSD = int.Parse(spinEdit_USD.EditValue.ToString());
            c.Money = int.Parse(spinEdit_VND.EditValue.ToString());
            c.Note = memoEdit_note.Text;
           ID = RegisterService.RegisterServices.CashBookFirstPeriodService.Save(c);
           if (ID > 0)
           {
               MessageControl.ShowMessage("Đã lưu thành công", "", MessageControl.Icon.Success);
               commandBarUC1.cmdsave.Enabled = false;
               LoadData();
               Focus();
           }
           else
           {
               MessageControl.ShowMessage("Lỗi vui lòng thử lại", "", MessageControl.Icon.Error);
           }
        }
        private void cmdrefresh_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            LoadData();
            Focus();
        }


        void DisPlay()
        {
            c = new CashBookFirstPeriod();
            c =  (CashBookFirstPeriod)RegisterService.RegisterServices.CashBookFirstPeriodService.GetByID(ID);
            if (c == null)
            {
                c = new CashBookFirstPeriod();
                ID = 0;
            }
            comboBoxEdit_Month.EditValue = c.Month;
            comboBoxEdit_Year.EditValue = c.Year;
            spinEdit_USD.EditValue = c.MoneyUSD;
            spinEdit_VND.EditValue = c.Money;
            memoEdit_note.Text = c.Note;
        }

        void FocusRow()
        {
            int i = 0;

            try
            {
                for (; i < gridView1.RowCount; i++)
                {
                    if ((long)gridView1.GetRowCellValue(i, "ID") == ID)
                    {
                        gridView1.FocusedRowHandle = i;
                    }
                }
            }
            catch (Exception)
            {

                //throw;
            }
        }


        public long ID { get; set; }
        public int MoneyVN { get; set; }
        public int MoneyUSD { get; set; }
        public string Note { get; set; }
        void LoadData()
        {
            var model = RegisterService.RegisterServices.CashBookFirstPeriodService;
            var data = (List<CashBookFirstPeriod>)model.GetList();

            if (data == null)
            {
                data = new List<CashBookFirstPeriod>();
            }
            else
            {
                data = data.OrderByDescending(c => c.Year).ToList() ;
            }


            gridControl1.DataSource = data;
            gridControl1.RefreshDataSource();
            gridView1.ViewCaption = "Danh sách có " + gridView1.RowCount + " dòng";

            PropertyInfo[] propertyInfos;
            propertyInfos = new CashBookFirstPeriod().GetType().GetProperties(); ;

            foreach (PropertyInfo proInfo in propertyInfos)
            {
                AttributeCollection attributes = TypeDescriptor.GetProperties(new CashBookFirstPeriod().GetType())[proInfo.Name].Attributes;

                DescriptionAttribute myAttribute =
                   (DescriptionAttribute)attributes[typeof(DescriptionAttribute)];
                if (String.IsNullOrEmpty(myAttribute.Description))
                {
                    gridView1.Columns[proInfo.Name].Visible = false;
                }
                else
                {
                    gridView1.Columns[proInfo.Name].Caption = myAttribute.Description;
                    gridView1.Columns[proInfo.Name].OptionsColumn.AllowEdit = false;
                }
            }
           // gridView1.Columns["SelectRowExt"].Visible = false;
             gridView1.Columns["Year"].GroupIndex = 0;
             gridView1.Columns["Month"].GroupIndex = 1;
            gridView1.ExpandAllGroups();

        }

        int FocusedRow
        {
            get
            {
                return gridView1.FocusedRowHandle;
            }
        }

        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            if (gridView1.RowCount != 0)
            {
                try
                {
                    ID = long.Parse(gridView1.GetRowCellValue(FocusedRow, "ID").ToString());
                    MoneyVN = int.Parse((gridView1.GetRowCellValue(FocusedRow, "Money").ToString()));
                    MoneyUSD = int.Parse(gridView1.GetRowCellValue(FocusedRow, "MoneyUSD").ToString());
                    Note = gridView1.GetRowCellValue(FocusedRow, "Note").ToString();
                }
                catch { }
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
            }
        }

        private void comboBoxEdit_Month_EditValueChanged(object sender, EventArgs e)
        {
            commandBarUC1.cmdsave.Enabled = true;
        }

        private void comboBoxEdit_Year_EditValueChanged(object sender, EventArgs e)
        {
            commandBarUC1.cmdsave.Enabled = true;
        }

        private void spinEdit_VND_EditValueChanged(object sender, EventArgs e)
        {
            commandBarUC1.cmdsave.Enabled = true;
        }

        private void spinEdit_USD_EditValueChanged(object sender, EventArgs e)
        {
            commandBarUC1.cmdsave.Enabled = true;
        }

        private void memoEdit_note_TextChanged(object sender, EventArgs e)
        {
            commandBarUC1.cmdsave.Enabled = true;
        }

        private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            try
            {
                if (gridView1.RowCount != 0)
                {
                    ID = long.Parse(gridView1.GetRowCellValue(e.FocusedRowHandle, "ID").ToString());

                }
            }
            catch { }
            DisPlay();
        }


       


       
    }
}
